package com.gp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gp.common.utils.CookieUtils;
import com.gp.mapper.BaseAibExchangeLogsMapper;
import com.gp.mapper.BaseAlbApplyMapper;
import com.gp.mapper.BaseUserExtendsMapper;
import com.gp.model.dto.AlbApplyVo;
import com.gp.model.dto.AuditApplyVo;
import com.gp.model.entity.BaseAibExchangeLogs;
import com.gp.model.entity.BaseAlbApply;
import com.gp.model.entity.BaseUser;
import com.gp.model.entity.BaseUserExtends;
import com.gp.service.IBaseAlbApplyService;

/**
 * @date: 2018年1月9日
 * @author: 弓成龙
 * @email: gcx909109@sina.cn
 * @description: 艾乐币的服务类
 **/
@Service
public class AlbServiceImpl extends ServiceImpl<BaseAlbApplyMapper, BaseAlbApply>
    implements IBaseAlbApplyService {

  @Autowired
  private BaseAibExchangeLogsMapper albLogMapper;
  @Autowired
  private BaseAlbApplyMapper albApplyMapper;
  @Autowired
  private BaseUserExtendsMapper userExtendsMapper;

  /**
   * 处理用户提交的兑换申请
   * 
   * @param submitApply 用户提交的申请
   */
  public boolean addSubmitApply(AlbApplyVo submitApply, Long uid, String username) {
    BaseAlbApply apply = new BaseAlbApply(submitApply.getApplyMoney(), uid, username);
    return albApplyMapper.insert(apply) == 1;
  }

  /**
   * 处理用户提交的体现申请
   * 
   * @param submitApply 用户提交的申请
   */
  public boolean submitWithdrawApply(AlbApplyVo submitApply, Long uid, String username) {
    BaseAlbApply apply = new BaseAlbApply(submitApply.getApplyMoney(), uid, username);
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("USERID", uid);
    // 2. 用户账户减少对应的金额
    BaseUserExtends user = ((List<BaseUserExtends>) userExtendsMapper.selectByMap(map)).get(0);
    int money = user.getCountMany();
    user.setCountMany(money - submitApply.getApplyMoney());
    if (money - submitApply.getApplyMoney() < 0 || userExtendsMapper.updateById(user) != 1) {
      return false;
    }
    // 1. 首先将申请持久化到申请表，设置申请状态为“wait_audit”
    Integer flag = albApplyMapper.insert(apply);
    return true;
  }

  /**
   * 检索用户所有的艾乐币交易记录
   * 
   * @param userId
   * @return
   */
  public Page listUserTradeRecord(Page page, String userId) {
    Map<String, Object> columnMap = new HashMap<String, Object>();
    columnMap.put("userId", userId);
    List<BaseAibExchangeLogs> map = albLogMapper.selectByMap(columnMap);
    return page.setRecords(map);
  }

  /**
   * 按照条件检索等待审核的申请
   * 
   * @param page 分页检索条件
   * @return PageInfo
   */
  public Page listWaitAuditApply(Page page) {
    Map<String, Object> columnMap = new HashMap<String, Object>();
    columnMap.put("state", "0");
    page.setRecords(albApplyMapper.selectByMap(columnMap));
    return page;
  }

  /**
   * 审核申请
   * 
   * @param auditApply
   * @return shifou
   */
  public boolean auditApply(AuditApplyVo auditApply) {
    BaseUser auditor = (BaseUser) CookieUtils.obtainCurrentUser(CookieUtils.getCurrentRequest());
    // 1. 处理申请
    BaseAlbApply apply = albApplyMapper.selectById(auditApply.getApplyId());
    apply.setAuditResult(auditApply.getIsSuccess().toString());
    apply.setAuditTime(new Date());
    if (auditor != null) {
      apply.setAuditor(auditor.getLoginName());
      apply.setAuditorId(auditor.getId().toString());
    }
    apply.setState(1);
    apply.setAuditMessage(auditApply.getMessage());
    albApplyMapper.updateById(apply);
    // 2. 处理申请结果 （如果申请成功，发放艾乐币到用户的账户上）
    if (auditApply.getIsSuccess() == 1) {
      Map<String, Object> map = new HashMap();
      map.put("userId", apply.getUserId());
      BaseUserExtends user = ((List<BaseUserExtends>) userExtendsMapper.selectByMap(map)).get(0);
      int many = user.getCountMany();
      user.setCountMany(many + apply.getMoney());
      userExtendsMapper.updateById(user);
    }
    return true;
  }

}
